package com.zhou.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;

import android.content.Context;
import android.content.res.AssetManager;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;

public class AndroidIOUtil {

	public static final String DB_NAME = "waimai"; // 保存的数据库文件名
	public static final String PACKAGE_NAME = "com.zhou.yuefan";
	public static final String DATABASE_PATH = "/data"
			+ Environment.getDataDirectory().getAbsolutePath() + "/"
			+ PACKAGE_NAME + "/databases"; // 在手机里存放数据库的位置

	/**
	 * 知道文件名后，从assets中获取Image文件，转化为Bitmap对象
	 * 
	 * @param 文件名
	 *            ，形式为：文件夹/文件名.后缀
	 * @return Bitmap 返回的Bitmap对象
	 */
	public static Bitmap getImageFromAssetsFile(Context context, String fileName) {
		Bitmap bitmap = null;
		AssetManager assetManager = context.getResources().getAssets();

		try {
			InputStream inputStream = assetManager.open(fileName);
			bitmap = BitmapFactory.decodeStream(inputStream);
			inputStream.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return bitmap;
	}

	public static void CreateDatabase(Context context, String dbFileName) {
		Log.i("liucheng", "CreateDatabase(Context context, String dbFileName)");
		try {
			// 获得dictionary.db文件的绝对路径
			String databaseFilename = DATABASE_PATH + "/" + dbFileName;
			Log.i("db_filename", databaseFilename);
			File dir = new File(DATABASE_PATH);
			Log.i("db_path", DATABASE_PATH);
			// 如果/sdcard/dictionary目录中存在，创建这个目录
			if (!dir.exists())
				dir.mkdir();
			// 如果在/sdcard/dictionary目录中不存在
			// dictionary.db文件，则从res\raw目录中复制这个文件到
			// SD卡的目录（/sdcard/dictionary）
			if (!(new File(databaseFilename)).exists()) {
				// 获得封装dictionary.db文件的InputStream对象
				AssetManager assetManager = context.getResources().getAssets();
				// InputStream is = context.getResources().openRawResource(
				// rawFileName);
				InputStream is = assetManager.open(dbFileName);
				FileOutputStream fos = new FileOutputStream(databaseFilename);
				byte[] buffer = new byte[8192];
				int count = 0;
				// 开始复制dictionary.db文件
				while ((count = is.read(buffer)) > 0) {
					fos.write(buffer, 0, count);
					System.out.println(count);
				}

				fos.close();
				is.close();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		Log.i("liucheng",
				"CreateDatabase(Context context, String dbFileName)执行完毕");
	}

	public static SQLiteDatabase openDatabase(Context context, String dbFileName) {

		try {
			// 获得dictionary.db文件的绝对路径
			String databaseFilename = DATABASE_PATH + "/" + dbFileName;
			Log.i("db_filename", databaseFilename);
			File dir = new File(DATABASE_PATH);
			Log.i("db_path", DATABASE_PATH);
			// 如果/sdcard/dictionary目录中存在，创建这个目录
			if (!dir.exists())
				dir.mkdir();
			// 如果在/sdcard/dictionary目录中不存在
			// dictionary.db文件，则从res\raw目录中复制这个文件到
			// SD卡的目录（/sdcard/dictionary）
			if (!(new File(databaseFilename)).exists()) {
				// 获得封装dictionary.db文件的InputStream对象
				AssetManager assetManager = context.getResources().getAssets();
				// InputStream is = context.getResources().openRawResource(
				// rawFileName);
				InputStream is = assetManager.open(dbFileName);
				FileOutputStream fos = new FileOutputStream(databaseFilename);
				byte[] buffer = new byte[8192];
				int count = 0;
				// 开始复制dictionary.db文件
				while ((count = is.read(buffer)) > 0) {
					fos.write(buffer, 0, count);
					System.out.println(count);
				}

				fos.close();
				is.close();
			}
			// 打开/sdcard/dictionary目录中的dictionary.db文件
			SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(
					databaseFilename, null);
			return database;
		} catch (Exception e) {
		}
		return null;
	}

}
